package com.icode.core.shared;

import com.icode.core.dto.house.*;
import com.icode.core.dto.order.OrderDTO;
import com.icode.core.dto.order.ReFundDTO;
import com.icode.core.dto.user.MemberDTO;
import com.icode.core.dto.user.UsersInfoDTO;
import com.icode.core.model.house.*;
import com.icode.core.model.manage.Reserve;
import com.icode.core.model.manage.ReserveTime;
import com.icode.core.model.user.City;
import com.icode.core.model.user.District;
import com.icode.core.model.user.Province;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-9
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */
public class DataToJson {
    private static String CHARACTER = "至";

    private static ObjectMapper mapper = new ObjectMapper();

    public static void ProvinceToJson(List<Province> list, HttpServletResponse response) {
        String content = "<option value=\"noselect\">请选择</option>";
        for (Province p : list) {
            content += "<option value=" + p.id() + ">" + p.name() + "</option>";
        }
        DataToJson(content, response);
    }

    public static void CityToJson(List<City> list, HttpServletResponse response) {
        String content = "<option value=\"noselect\">请选择</option>";
        for (City c : list) {
            content += "<option value=" + c.id() + ">" + c.name() + "</option>";
        }
        DataToJson(content, response);
    }

    public static void DistrictToJson(List<District> list, HttpServletResponse response) {
        String content = "<option value=\"noselect\">请选择</option>";
        for (District d : list) {
            content += "<option value=" + d.id() + ">" + d.name() + "</option>";
        }
        DataToJson(content, response);
    }

    public static void DataToJson(String content, HttpServletResponse response) {
        try {
            PrintWriter out = response.getWriter();
            String json = mapper.writeValueAsString(content);
            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void refundDataToJson(List<ReFundDTO> reFundDTOs, HttpServletResponse response) {
        String content = "";
        if (!reFundDTOs.isEmpty()) {
            for (int i = 0; i < reFundDTOs.size(); i++) {
                content += "<tr><td><input type=\"checkbox\" value=\"" + reFundDTOs.get(i).getGuid() + "\" id=\"sel_" + i + "\"></td>";
                content += "<td>" + reFundDTOs.get(i).getTradeNo() + "</td>";
                content += "<td>" + reFundDTOs.get(i).getPrice() + "</td>";
                content += "<td>" + reFundDTOs.get(i).getReason() + "</td>";
                content += "<td id=\"status_" + i + "\">" + reFundDTOs.get(i).getRefundStatus() + "</td>";
                if ("待审核".equals(reFundDTOs.get(i).getRefundStatus())) {
                    content += "<td><a href=\"/order/reject/" + reFundDTOs.get(i).getGuid() + "\" id=\"fail_" + i + "\">驳回</a>&nbsp;";
                    content += "<a href=\"/order/doReFund/" + reFundDTOs.get(i).getGuid() + "\" id=\"refund_" + i + "\">退款</a>";
                } else {
                    content += "<td><a href=\"javascript:void(0)\" id=\"fail_" + i + "\">驳回</a>&nbsp;";
                    content += "<a href=\"javascript:void(0)\" id=\"refund_" + i + "\">退款</a>";
                }
                content += "</td></tr>";
            }
            DataToJson(content, response);
        } else {
            DataToJson(content, response);
        }
    }

    public static void memberDataToJson(List<MemberDTO> memberDTOs, HttpServletResponse response) {
        String content = "";
        if (!memberDTOs.isEmpty()) {
            for (int i = 0; i < memberDTOs.size(); i++) {
                content += "<tr><td>" + (i + 1) + "</td>";
                content += "<td>" + memberDTOs.get(i).getName() + "</td>";
                content += "<td>" + memberDTOs.get(i).getGender() + "</td>";
                content += "<td>" + memberDTOs.get(i).getIdCard() + "</td>";
                content += "<td>" + memberDTOs.get(i).getPhoneNum() + "</td>";
                content += "<td>" + memberDTOs.get(i).getInitiateDate() + "</td>";
                content += "<td>" + memberDTOs.get(i).getIdCardAddress() + "</td>";
                content += "<td><a href=\"/member/show/" + memberDTOs.get(i).getGuid() + "\">详情信息</a>&nbsp;";
                content += "<a href=\"/member/delete/" + memberDTOs.get(i).getGuid() + "\">删除</a></td></tr>";
            }
            DataToJson(content, response);
        } else {
            DataToJson("无满足条件的数据!", response);
        }
    }

    public static void houseBaseAreaToJson(List<ForEnumDTO> list, HttpServletResponse response) {
        String content = "";
        for (ForEnumDTO f : list) {
            content += "<option value=" + f.getName() + ">" + f.getLabel() + "</option>";
        }
        DataToJson(content, response);
    }

    public static void homeIntroImageToJson(List<HomeIntroImage> homeIntroImages, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < homeIntroImages.size(); i++) {
            content += "<img style=width:50px;height:70px src=/public/image/" + homeIntroImages.get(i).image().guid() + " id=introImg_" + i + " onmouseover=mouseover('introImg_" + i + "') onmouseout=mouseout('introImg_" + i + "') >";
            content += "<img src=/public/img/x.png class=delImg onclick=delImg('intro','" + homeIntroImages.get(i).image().guid() + "') style=margin-top:-35px;margin-left:-39px;opacity:0.8;  id=delIntroImg_" + i + " >";
        }
        DataToJson(content, response);
    }

    public static void homeInfoImageToJson(List<HomeInfoImage> homeInfoImages, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < homeInfoImages.size(); i++) {
            content += "<img style=width:50px;height:70px src=/public/image/" + homeInfoImages.get(i).image().guid() + " id=infoImg_" + i + " onmouseover=infomouseover('infoImg_" + i + "') onmouseout=infomouseout('infoImg_" + i + "') >";
            content += "<img src=/public/img/x.png class=delImg onclick=delImg('info','" + homeInfoImages.get(i).image().guid() + "') style=margin-top:-35px;margin-left:-39px;opacity:0.8;  id=delInfoImg_" + i + " >";
        }
        DataToJson(content, response);
    }

    public static void homePartImageToJson(List<HomePartnerImage> homePartnerImages, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < homePartnerImages.size(); i++) {
            content += "<img style=width:100px;height:50px; src=/public/image/" + homePartnerImages.get(i).image().guid() + " id=partnersImg_" + i + " onmouseover=partmouseover('partnersImg_" + i + "') onmouseout=partmouseout('partnersImg_" + i + "') >";
            content += "<img src=/public/img/x.png class=delImg onclick=delImg('partners','" + homePartnerImages.get(i).image().guid() + "') style=margin-top:-35px;margin-left:-39px;opacity:0.8;  id=delPartnersImg_" + i + " >";
        }
        DataToJson(content, response);
    }

    public static void houseInfoByAreaToJson(List<HouseInfoDTO> houseInfoDTOs, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < houseInfoDTOs.size(); i++) {
            content += "<a href=javascript:void(0) class=houseInfo><input type=hidden value=" + houseInfoDTOs.get(i).getGuid() + " />" + houseInfoDTOs.get(i).getName() + "</a>&nbsp;";
        }
        DataToJson(content, response);
    }

    public static void houseWeekByAreaToJson(List<HouseWeekDTO> houseWeekDTOs, HttpServletResponse response) {
        String content = "";
        Iterator<HouseWeekDTO> iterator = houseWeekDTOs.iterator();
        int number = 1;
        while (iterator.hasNext()) {
            HouseWeekDTO houseWeekDTO = iterator.next();
            content += "<tr><td>" + number++ + "</td><td>" + houseWeekDTO.getName() + "</td><td>" + houseWeekDTO.getPrice() + "</td><td>" + houseWeekDTO.getTotalNum() +
                    "</td><td>" + houseWeekDTO.getSoldNum() + "</td><td>" + houseWeekDTO.getDescription() + "</td><td>" + "<button onclick=update('" + houseWeekDTO.getGuid() + "')>修改</button>" + "<button onclick=deletehouseweek('" + houseWeekDTO.getGuid() + "')>删除</button>" + "</td></tr>";
        }
        DataToJson(content, response);
    }

    public static void houseInfoForHouseWeekTime(List<HouseInfoDTO> houseInfoDTOs, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < houseInfoDTOs.size(); i++) {
            content += "<a href=javascript:houseInfoChange('" + houseInfoDTOs.get(i).getGuid() + "')>" + houseInfoDTOs.get(i).getName() + "</a>";
        }
        DataToJson(content, response);
    }

    public static void houseWeekTypeToJson(List<HouseWeekDTO> houseWeekDTOs, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < houseWeekDTOs.size(); i++) {
            content += "<option value=" + houseWeekDTOs.get(i).getGuid() + ">" + houseWeekDTOs.get(i).getName() + "</option>";
        }
        DataToJson(content, response);
    }

    public static void houseWeekTimeToJson(List<HouseWeekTime> houseWeekTimes, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < houseWeekTimes.size(); i++) {
            content += "<tr><td><input type=checkbox value=" + houseWeekTimes.get(i).guid() + "></td>";
            content += "<td>" + houseWeekTimes.get(i).houseWeek().name() + "</td>";
            content += "<td>" + houseWeekTimes.get(i).houseWeek().houseInfo().baseInfo().houseName() + "</td>";
            content += "<td>" + houseWeekTimes.get(i).houseWeek().price() + "</td>";
            content += "<td>" + houseWeekTimes.get(i).start() + "</td>";
            content += "<td>" + houseWeekTimes.get(i).end() + "</td>";
            content += "<td>" + houseWeekTimes.get(i).month() + "</td></tr>";
        }
        DataToJson(content, response);
    }

//    public static void houseWeekTimeByMonthToJson(List<HouseWeekTimeDTO> houseWeekTimeDTOs, HttpServletResponse response) {
//        String content = "";
//        for (int i = 0; i < houseWeekTimeDTOs.size(); i++) {
//            HouseWeekTimeDTO houseWeekTime = houseWeekTimeDTOs.get(i);
//            String start = houseWeekTime.getStart();
//            String end = houseWeekTime.getEnd();
//            String weekTime = start + CHARACTER + end;
//
//            content += "<div class=week>";
//            content += "<input type=checkbox class=checkBox id=checkBoxInput_" + (i + 1) + "  value=" + houseWeekTimeDTOs.get(i).getGuid() + ">";
//            content += "<label title=" + weekTime + " for=checkBoxInput_" + (i + 1) + "  class=weekText>第" + (i + 1) + "周</label></div>";
//        }
//        DataToJson(content, response);
//    }

    public static void houseWeekTimeByMonthToJson(List<HouseWeekTimeDTO> houseWeekTimeDTOs, HttpServletResponse response) {
        String content = "";
        for (int i = 0; i < houseWeekTimeDTOs.size(); i = i + 2) {
            content += "<tr>";
            HouseWeekTimeDTO houseWeekTime1 = houseWeekTimeDTOs.get(i);
            String start1 = houseWeekTime1.getStart();
            String end1 = houseWeekTime1.getEnd();
            content += "<td>时间:</td><td><label>" + start1 + "至" + end1 + "</label><input class=\"week\" type=\"checkbox\" value=\"" + houseWeekTime1.getGuid() + "\"></td>";
            if (i + 1 < houseWeekTimeDTOs.size()) {
                HouseWeekTimeDTO houseWeekTime2 = houseWeekTimeDTOs.get(i + 1);
                String start2 = houseWeekTime2.getStart();
                String end2 = houseWeekTime2.getEnd();
                content += "<td>时间:</td><td><label>" + start2 + "至" + end2 + "</label><input class=\"week\" type=\"checkbox\" value=\"" + houseWeekTime2.getGuid() + "\"></td>";
            }
            content += "</tr>";
        }
        DataToJson(content, response);
    }

    public static void toBuildTypes(List<BuildingTypeDTO> buildingTypeDTOs, HttpServletResponse response) {
//        StringBuffer html = new StringBuffer();
//        html.append("<ul class=\"clearfix\" > ");
//        for (BuildingTypeDTO dto : buildingTypeDTOs) {
//            html.append(" <li>");
//            html.append("<a href=\"javascript:\">").append("<img src=\"/public/image/" + dto.getImageGuids().get(0) + "\" width=\"320\" height=\"210\">").append("</a>");
//            html.append("<div class=\"DescribleOfPicture\">");
//            html.append("<a>房型：").append(dto.getHouseTypeAsString()).append("</a>");
//            html.append("<a>可住人数：").append(dto.getPersonNum()).append("</a>");
//            html.append("<a>建筑面积：").append(dto.getBuildingArea()).append("</a>");
//            html.append("<a>赠送面积：").append(dto.getExtraArea()).append("</a>");
//            html.append("<a>实际面积：").append(dto.getArea()).append("</a>").append("</div>");
//            html.append("</li>");
//        }
//        html.append("</ul>");
//        return html.toString();
        //*****************************
        String content = "";
        content += "<ul class=clearfix>";
        for (BuildingTypeDTO dto : buildingTypeDTOs) {
            String guid = dto.getGuid();
            content += "<li>";
            content += "<a href=/front/houseBase/showHouseInfo/" + guid + "><img src=/public/image/" + dto.getImageGuids().get(0) + " width=320 height=210></a>";
            content += "<div class=DescribleOfPicture>";
            content += "<a>房型：" + dto.getHouseTypeAsString() + "</a>";
            content += "<a>可住人数：" + dto.getPersonNum() + "</a>";
            content += "<a>建筑面积：" + dto.getBuildingArea() + "</a>";
            content += "<a>赠送面积：" + dto.getExtraArea() + "</a>";
            content += "<a>实际面积：" + dto.getArea() + "</a></div>";
            content += "</li>";
        }
        content += "</ul>";
        DataToJson(content, response);
    }

    public static void createWeekTimesByReserve(Reserve reserve, HttpServletResponse response) {
        List<ReserveTime> reserveTimes = reserve.reserveTimes();
        for (ReserveTime reserveTime : reserveTimes) {
            reserveTime.houseWeekTime().start();
            reserveTime.houseWeekTime().end();
            reserveTime.houseWeekTime().month();
        }

    }

    public static void userInfoToJson(List<UsersInfoDTO> usersInfoDTOs, HttpServletResponse response) {
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < usersInfoDTOs.size(); i++) {
            content.append("<tr><td><p name=\"username\">" + usersInfoDTOs.get(i).getUsername() + "</p></td>");
            content.append("<td><p name=\"realName\">" + usersInfoDTOs.get(i).getRealName() + "</p></td>");
            content.append("<td><p name=\"gender\">" + usersInfoDTOs.get(i).getGender() + "</p></td>");
            content.append("<td><p name=\"IDCard\">" + usersInfoDTOs.get(i).getIDCard() + "</p></td>");
            content.append("<td><p name=\"phoneNum\">" + usersInfoDTOs.get(i).getPhoneNum() + "</p></td>");
            content.append("<td><p name=\"email\">" + usersInfoDTOs.get(i).getEmail() + "</p></td>");
            content.append("<td><p name=\"activate\">" + usersInfoDTOs.get(i).getActivate() + "</p></td>");
            content.append("<td>");
            if (usersInfoDTOs.get(i).getActivate().equals("true")) {
                content.append("<a href=\"javascript:changeUserStatus('" + usersInfoDTOs.get(i).getPhoneNum() + "','false')\" title=\"用户状态\">关闭</a>&nbsp;");
            } else {
                content.append("<a href=\"javascript:changeUserStatus('" + usersInfoDTOs.get(i).getPhoneNum() + "','true')\" title=\"用户状态\">开启</a>&nbsp;");
            }
            content.append("<a href=\"www.baidu.com\" title=\"详细信息\">详细信息</a>");
            content.append("</td>");
            content.append("</tr>");
        }
        DataToJson(content.toString(), response);
    }

    public static void orderInfoTojson(List<OrderDTO> orderDTOs, HttpServletResponse response) {
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < orderDTOs.size(); i++) {
            content.append("<tr><td>" + orderDTOs.get(i).getHouseInfoDTO().getName() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getHouseWeekDTO().getName() + "</td>");
            content.append("<td>￥" + orderDTOs.get(i).getHouseWeekDTO().getPrice() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getHouseWeekDTO().getHouseBaseArea() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getRealName() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getPhoneNum() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getOrderInfoDTO().getStatus() + "</td>");
            content.append("<td>" + orderDTOs.get(i).getOrderInfoDTO().getTime() + "</td>");
            if (orderDTOs.get(i).getOrderInfoDTO().getAlipayTradeNo() == null){
                content.append("<td>无数据</td>");
            }
            else {
                content.append("<td>" + orderDTOs.get(i).getOrderInfoDTO().getAlipayTradeNo() + "</td>");
            }
            content.append("</tr>");
        }
        DataToJson(content.toString(), response);
    }
}
